package com.sync.platform.util;

/**
 * Utility class to build JDBC URLs for different database types
 */
public class JdbcUrlBuilder {

    /**
     * Build JDBC URL based on database type
     *
     * @param dbType Database type (MYSQL, POSTGRESQL, ORACLE, SQLSERVER)
     * @param host Database host
     * @param port Database port
     * @param databaseName Database name
     * @param charset Character set (optional, can be null)
     * @return JDBC URL
     */
    public static String buildJdbcUrl(String dbType, String host, int port, String databaseName, String charset) {
        if (dbType == null || host == null || databaseName == null) {
            throw new IllegalArgumentException("Database type, host, and database name cannot be null");
        }

        String url;
        switch (dbType.toUpperCase()) {
            case "MYSQL":
                url = buildMySqlUrl(host, port, databaseName, charset);
                break;
            case "POSTGRESQL":
                url = buildPostgreSqlUrl(host, port, databaseName);
                break;
            case "ORACLE":
                url = buildOracleUrl(host, port, databaseName);
                break;
            case "SQLSERVER":
                url = buildSqlServerUrl(host, port, databaseName);
                break;
            default:
                throw new IllegalArgumentException("Unsupported database type: " + dbType);
        }
        return url;
    }

    /**
     * Get JDBC driver class name based on database type
     *
     * @param dbType Database type (MYSQL, POSTGRESQL, ORACLE, SQLSERVER)
     * @return JDBC driver class name
     */
    public static String getDriverClassName(String dbType) {
        if (dbType == null) {
            throw new IllegalArgumentException("Database type cannot be null");
        }

        switch (dbType.toUpperCase()) {
            case "MYSQL":
                return "com.mysql.cj.jdbc.Driver";
            case "POSTGRESQL":
                return "org.postgresql.Driver";
            case "ORACLE":
                return "oracle.jdbc.driver.OracleDriver";
            case "SQLSERVER":
                return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            default:
                throw new IllegalArgumentException("Unsupported database type: " + dbType);
        }
    }

    private static String buildMySqlUrl(String host, int port, String databaseName, String charset) {
        StringBuilder url = new StringBuilder("jdbc:mysql://")
                .append(host)
                .append(":")
                .append(port)
                .append("/")
                .append(databaseName);

        // Add parameters
        url.append("?useSSL=false&serverTimezone=UTC");
        
        // Ensure we can properly select the catalog/database 
        url.append("&nullCatalogMeansCurrent=true");

        // Add charset if specified
        if (charset != null && !charset.isEmpty()) {
            url.append("&characterEncoding=").append(charset);
        }

        return url.toString();
    }

    private static String buildPostgreSqlUrl(String host, int port, String databaseName) {
        return String.format("jdbc:postgresql://%s:%d/%s", host, port, databaseName);
    }

    private static String buildOracleUrl(String host, int port, String databaseName) {
        // Oracle SID connection
        return String.format("jdbc:oracle:thin:@%s:%d:%s", host, port, databaseName);
    }

    private static String buildSqlServerUrl(String host, int port, String databaseName) {
        return String.format("jdbc:sqlserver://%s:%d;databaseName=%s", host, port, databaseName);
    }
}
